package com.taotao.order.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.ExceptionUtil;
import com.taotao.order.pojo.Order;
import com.taotao.order.service.OrderService;

@Controller
public class OrderController {

	@Autowired
	private OrderService orderService;

	@RequestMapping("/create")
	@ResponseBody
	public TaotaoResult createOrder(@RequestBody Order order) {

		if (order == null || order.getOrderShipping() == null || order.getOrderItems() == null)
			return TaotaoResult.build(400, "参数错误");
		if(order.getUserId()==null)
			return TaotaoResult.build(400, "用户id不能为空");
		{
			TaotaoResult result;
			try {
				result = orderService.createOrder(order);
			} catch (Exception e) {

				String trace = ExceptionUtil.getStackTrace(e);
				e.printStackTrace();
				return TaotaoResult.build(500, trace);
			}
			return result;

		}
	}

	@RequestMapping("/info/{orderId}")
	@ResponseBody
	public TaotaoResult createOrder(@PathVariable String orderId) {
		if (StringUtils.isBlank(orderId))
			return TaotaoResult.build(400, "参数错误");
		TaotaoResult result = null;
		try {
			result = orderService.getOrderById(orderId);
		} catch (Exception e) {

			String trace = ExceptionUtil.getStackTrace(e);
			e.printStackTrace();
			return TaotaoResult.build(500, trace);
		}
		return TaotaoResult.ok(result);

	}
}
